package org.cupk2.wdap.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.cupk2.base.db.CupkDbFields;
import org.cupk2.base.domain.CupkUserContextLoader;
import org.cupk2.base.exception.CupkExceptionUtils;
import org.cupk2.tools.caches.CupkCacheKeys;
import org.cupk2.tools.caches.CupkCaches;
import org.cupk2.tools.db.commons.DBRule;
import org.cupk2.tools.db.commons.DBRuleList;
import org.cupk2.wdap.dao.WdapExtendFileDao;
import org.cupk2.wdap.domain.WdapExtendFile;
import org.cupk2.wdap.service.WdapExtendFileServiceInter;
import org.cupk2.tools.db.DataQuery;
import org.cupk2.tools.db.DataResult;
import org.cupk2.tools.i18n.I18n;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 扩展附件
 *
 * @author cbtg自动生成  2025-1-25 18:24:40
 */
@Service
@Slf4j
public class WdapExtendFileServiceImpl implements WdapExtendFileServiceInter {


    @Autowired
    private WdapExtendFileDao wdapExtendFileDao;

    @Transactional
    @Override
    public WdapExtendFile insertWdapExtendFileEntity(WdapExtendFile wdapExtendFile) {
        CupkDbFields.initInsertBean(wdapExtendFile, CupkUserContextLoader.getCurrentUser());

        wdapExtendFileDao.insert(wdapExtendFile);


        return wdapExtendFile;
    }

    @Transactional
    @Override
    public WdapExtendFile editWdapExtendFileEntity(WdapExtendFile wdapExtendFile) {
        WdapExtendFile saveWdapExtendFile = getWdapExtendFileById(wdapExtendFile.getId());
        CupkExceptionUtils.throwNullEx(saveWdapExtendFile, I18n.msg("扩展附件不存在:?", wdapExtendFile.getId()));
        saveWdapExtendFile.setId(wdapExtendFile.getId());
        saveWdapExtendFile.setTaskid(wdapExtendFile.getTaskid());
        saveWdapExtendFile.setFileid(wdapExtendFile.getFileid());
        saveWdapExtendFile.setFilemodel(wdapExtendFile.getFilemodel());
        saveWdapExtendFile.setViewis(wdapExtendFile.getViewis());
        saveWdapExtendFile.setState(wdapExtendFile.getState());
        saveWdapExtendFile.setNote(wdapExtendFile.getNote());
        saveWdapExtendFile.setResourcekey(wdapExtendFile.getResourcekey());
        saveWdapExtendFile.setServerid(wdapExtendFile.getServerid());

        CupkDbFields.initUpdateBean(saveWdapExtendFile, CupkUserContextLoader.getCurrentUser());
        wdapExtendFileDao.update(saveWdapExtendFile);
        return saveWdapExtendFile;
    }

    @Transactional
    @Override
    public WdapExtendFile getWdapExtendFileById(String id) {
        WdapExtendFile extendFile = CupkCaches.getInstance().getCacheData(id, CupkCacheKeys.EXTEND_FILE);
        if (extendFile == null) {
            extendFile = wdapExtendFileDao.findById(id);
            CupkCaches.getInstance().putCacheData(id, extendFile, CupkCacheKeys.EXTEND_FILE);
        }
        return extendFile;
    }

    @Override
    public List<WdapExtendFile> getWdapExtendFiles(DataQuery query) {
        return wdapExtendFileDao.queryData(query.setCount(false)).getData(WdapExtendFile.class);
    }


    @Transactional
    @Override
    public DataResult searchWdapExtendFile(DataQuery query) {
        DataResult result = wdapExtendFileDao.queryData(query);
        return result;
    }

    @Override
    public int getWdapExtendFileNum(DataQuery query) {
        return wdapExtendFileDao.countData(query);
    }


    @Transactional
    @Override
    public void delWdapExtendFile(String id) {
        /*[tree：树形结构使用]
        if ( wdapExtendFileDao.findByParentId(id).size() > 0) {
            throw new RuntimeException("不能删除该节点，请先删除其子节点");
        }
        */
        wdapExtendFileDao.deleteById(id);
    }

    @Override
    public int getNum(DataQuery query) {
        return wdapExtendFileDao.countData(query);
    }

    @Override
    @Transactional
    public void clearExtendFileByTask(String taskid) {
        wdapExtendFileDao.delete(DBRuleList.getInstance().add(new DBRule("TASKID", taskid, "=")).toList());
    }

    @Override
    public WdapExtendFile getWdapExtendFileByResourceFileId(String fileId, String fileModelkey) {
        WdapExtendFile extendFile = CupkCaches.getInstance().getCacheData(fileId+fileModelkey, CupkCacheKeys.EXTEND_FILE);
        if (extendFile == null) {
            List<WdapExtendFile> files = wdapExtendFileDao.find(DBRuleList.getInstance().add(new DBRule("FILEID", fileId, "=")).add(new DBRule("FILEMODEL", fileModelkey, "=")).toList());
            if (files.size() > 0) {
                extendFile = files.get(0);
                CupkCaches.getInstance().putCacheData(fileId+fileModelkey, extendFile, CupkCacheKeys.EXTEND_FILE);
            }
        }
        return extendFile;
    }

    @Override
    public List<WdapExtendFile> getWdapExtendFileByResourceFileId(String fileId) {
        List<WdapExtendFile> files = wdapExtendFileDao.find(DBRuleList.getInstance().add(new DBRule("FILEID", fileId, "=")).toList());
        return files;
    }
}
